import React, { useRef, useState } from 'react';

import _ from 'lodash';
import DataEdit from '@/components/base/DataEdit';
import { Button, message, PageHeader } from 'antd';
import { AddCompbldkDgh, DelCompbldkDgh } from '@/services/commCRUD';
import DataTable from '@/components/base/DataTable';

const daoName = 'ShBlkhDggl';
type PropField = {
  CUID: number;
};

const columns: API.Column[] = [
  {
    title: '客户号',
    width: 150,
    sorter: true,
    key: 'CUNO',
    dataIndex: 'CUNO',
    dispaly: true,
    filter: true,
  },
  {
    title: '组织机构代码',
    width: 150,
    sorter: true,
    key: 'ENCD',
    dataIndex: 'ENCD',
    dispaly: true,
    filter: true,
  },
  {
    title: '客户名称',
    width: 150,
    sorter: true,
    key: 'CUNM',
    dataIndex: 'CUNM',
    dispaly: true,
    filter: true,
  },
  {
    title: '启用日期',
    width: 150,
    sorter: true,
    key: 'cfc01setud',
    dataIndex: 'cfc01setud',
    dispaly: true,
    filter: true,
  },
  {
    title: '注册资金',
    width: 150,
    sorter: true,
    key: 'cfc01restm',
    dataIndex: 'cfc01restm',
    dispaly: true,
    filter: true,
  },
  {
    title: '法人代表',
    width: 150,
    sorter: true,
    key: 'cfc01leren',
    dataIndex: 'cfc01leren',
    dispaly: true,
    filter: true,
  },
  {
    title: '法人代表证件号',
    width: 150,
    sorter: true,
    key: 'cfc01lerec',
    dataIndex: 'cfc01lerec',
    dispaly: true,
    filter: true,
  },
  {
    title: '客户id',
    width: 150,
    sorter: true,
    key: 'CUID',
    dataIndex: 'CUID',
    dispaly: false,
    filter: true,
  },
  {
    title: '关联客户id',
    width: 150,
    sorter: true,
    key: 'GLCUID',
    dataIndex: 'GLCUID',
    dispaly: false,
    filter: true,
  },
];
const controls: API.Control[] = [
  {
    name: 'CUNO',
    rules: [{ required: true, message: '不能为空' }],
    type: 'text',
    label: '客户号',
    grid: 12 / 24,
  },
  {
    name: 'ENCD',
    rules: [{ required: true, message: '不能为空' }],
    type: 'text',
    label: '组织机构代码',
    grid: 12 / 24,
  },
  {
    name: 'CUNM',
    rules: [{ required: true, message: '不能为空' }],
    type: 'text',
    label: '客户名称',
    grid: 12 / 24,
  },
  {
    name: 'cfc01setud',
    rules: [{ required: true, message: '不能为空' }],
    type: 'text',
    label: '启用日期',
    grid: 12 / 24,
  },
  {
    name: 'cfc01restm',
    rules: [{ required: true, message: '不能为空' }],
    type: 'text',
    label: '注册资金',
    grid: 12 / 24,
  },
  {
    name: 'cfc01leren',
    rules: [{ required: true, message: '不能为空' }],
    type: 'text',
    label: '法人代表',
    grid: 12 / 24,
  },
  {
    name: 'cfc01lerec',
    rules: [{ required: true, message: '不能为空' }],
    type: 'text',
    label: '法人代表证件号',
    grid: 12 / 24,
  },
  {
    name: 'CUID',
    rules: [{ required: true, message: '不能为空' }],
    type: 'text',
    label: '客户id',
    grid: 12 / 24,
  },
  {
    name: 'GLCUID',
    rules: [{ required: true, message: '不能为空' }],
    type: 'text',
    label: '关联客户id',
    grid: 12 / 24,
  },
];
const listcontrols: API.Control[] = [
  {
    name: 'DGKH',
    rules: [{ required: false, message: '可以为空' }],
    type: 'search',
    label: '对公客户',
    grid: 18 / 24,
    layout: { labelSpan: 8, wrapperSpan: 15 },
    fetch: {
      tableName: 'ShBlkhDivi',
      searchColumns: ['ENCD', 'CUNM'],
      optionTpl: 'CUNM',
      valueTpl: 'CUID',
      isDao: true,
    },
  },
];

const ShBlkhDgglForm: React.FC<PropField> = (props) => {
  const tableRef = useRef<API.TableAction>();
  const [id, setId] = useState<number>(0);
  const submit = async (vals: Record<string, any>) => {
    AddCompbldkDgh(props.CUID, vals.DGKH)
      .then(function () {
        if (tableRef.current) {
          message.info('添加成功');
          tableRef.current?.reload();
        }
      })
      .catch(function (ex) {
        console.error('添加绩费参数出错', ex);
      });
  };
  return (
    <PageHeader>
      <div style={{ marginTop: -5, marginBottom: 10 }}>
        {' '}
        <DataEdit
          showType="inner"
          layout="inline"
          title="对公存款绩费系数"
          controls={listcontrols}
          onSumit={submit}
          renderButtons={(p) => {
            return [
              <Button type="primary" key="submit" onClick={() => p.form?.submit?.()}>
                添加
              </Button>,
              <Button
                type="default"
                key="rest"
                onClick={() => {
                  if (id === 0) {
                    message.info('请选择要删除的账户');
                    return;
                  }
                  DelCompbldkDgh(id)
                    .then(function () {
                      if (tableRef.current) {
                        message.info('删除成功');
                        tableRef.current?.reload();
                      }
                    })
                    .catch(function (ex) {
                      console.error('删除出错', ex);
                    });
                }}
              >
                删除
              </Button>,
            ];
          }}
        />
      </div>

      <DataTable
        daoName={daoName}
        columns={columns}
        controls={controls}
        actionRef={tableRef}
        keyColName={'GLCUID'}
        title="零售不良认定"
        pageSize={20}
        seeks={[
          {
            WhereItems: [{ FieldName: 'CUID', Operator: '=', FieldValues: '' + props.CUID }],
            RelationSign: 'AND',
          },
        ]}
        onSelectChange={async (keys) => {
          if (!_.isEmpty(keys)) {
            setId(keys[0] as number);
          }
        }}
      />
    </PageHeader>
  );
};

export default ShBlkhDgglForm;
ShBlkhDgglForm.defaultProps = {
  CUID: 0,
};
